package com.jubotech.business.web.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jubotech.business.web.domain.TaskTimeInfo;
import com.jubotech.business.web.domain.WxAccountInfo;
import com.jubotech.business.web.query.TaskTimeQuery;
import com.jubotech.business.web.service.TaskTimeService;
import com.jubotech.business.web.service.WxAccountService;
import com.jubotech.framework.common.BaseController;
import com.jubotech.framework.common.DispatchMode;
import com.jubotech.framework.common.GridPage;
import com.jubotech.framework.common.ResultInfo;
import com.jubotech.framework.util.MD5Util;
import com.jubotech.framework.util.SessionUtil;
@Controller
@RequestMapping("/user")
public class TasktimeController  extends BaseController<TaskTimeInfo> {

	@Autowired
	private TaskTimeService service;
	
	@Autowired
	private WxAccountService  wxAccountService;
	 
	@RequestMapping("/taskTime/{mode}/{type}/dispatch")
	public ModelAndView dispatch(HttpServletRequest request, @PathVariable String mode, @PathVariable String type, Integer id) {
		Map<String, Object> model = new HashMap<>();
		
		List<WxAccountInfo> list = wxAccountService.findWeChatAccountInfoByCid(SessionUtil.getUserCid(request));
		
		model.put("mode", mode);
		if (DispatchMode.LIST_MODE.equals(mode)) {
			return new ModelAndView("/user/taskTime/list", model);
		}
		if (DispatchMode.ADD_MODE.equals(mode)) {
			SessionUtil.setToken(request, MD5Util.makeToken());
			model.put("wechatlist", list);
			return new ModelAndView("/user/taskTime/"+type+"/edit", model);
		}
		if (DispatchMode.UPDATE_MODE.equals(mode)) {
			SessionUtil.setToken(request, MD5Util.makeToken());
			model.put("info", service.findTaskTimeInfoByid(id));
			model.put("wechatlist", list);
			return new ModelAndView("/user/taskTime/"+type+"/edit", model);
		}
		return new ModelAndView("/common/error");
	}

	@PostMapping("/taskTime/pageList")
	@ResponseBody
	public ResultInfo pageList(HttpServletRequest request, TaskTimeQuery query) {
		query.setCid(SessionUtil.getUserCid(request));
		GridPage<TaskTimeInfo> gridPage = new GridPage<>(service.pageList(query));
		return ResultInfo.success(gridPage);
	}

	@PostMapping("/taskTime/add")
	@ResponseBody
	public ResultInfo add(HttpServletRequest request, TaskTimeInfo info) {
		if (isRepeatSubmit(request)) {
			return ResultInfo.fail("请勿重复提交");
		}
		info.setCid(SessionUtil.getUserCid(request));
		return service.saveTask(info);
	}
 
	@PostMapping("/taskTime/deletes")
	@ResponseBody
	public ResultInfo deletes(Integer ids) {
		
		return service.delete(ids);
	}

}
